AMU
February 28, 2024
In R
, we can import data formats like ASCII, Excel, CSV and many more
Files with extensions
.txt
(files in text fomrat),.csv
(files in comma seperated values) and.xlxs
(files in excel formate)Lets learn one by one
Note: For more about file extensions (.txt, .csv, .xlxs) please GOOGLE
Two most commonly used methods for importing data
read.csv
read.csv
is used import data file with .csv
extension aka file in which data is stored with comma sepearted value
Let’s take an example
example.csv
read.table
functionArguments for the read.table
header = TRUE
read first line as column names
dec = ","
comma as decimal mark
sep = "_"
underscore as column separator (“ for tabstop)
fill = T
fill incomplete rows with NAs at the end
skip = 12
ignore the first 12 lines (eg with meta data)
read.table
functionexample.txt
In the above two methods, we have to give path of the data file stored/kept
Instead, we can use file.choose()
function
Setup a directory in not done using command setwd()
or know your directory using command getwd()
If Rproject
is being used no need of setting working directory
In R, we can write data frames easily to a file, using the write.table()
command.
First argument refers to the data frame to be written to the output file
the second is the name of the output file.
By default R will surround each entry in the output file by quotes, so we use quote=F
.
Use the below command, notice the difference
Excel
fileExcel
fileTHANKS